package service

import (
	"ruanGongKeShe16Zu/db"
	"ruanGongKeShe16Zu/model"
	"ruanGongKeShe16Zu/repository"
	"ruanGongKeShe16Zu/service/vo"
	"sync"

	"github.com/gin-gonic/gin"
)

// 单例实现
var OrderServiceInstance *OrderService
var OrderServiceOnce sync.Once

type OrderService struct {
	Repository repository.OrderRepository
}

func GetOrderService() *OrderService {
	OrderServiceOnce.Do(func() {
		repository := repository.NewOrderRepository().(repository.OrderRepository)
		repository.DB.AutoMigrate(&model.Order{})
		OrderServiceInstance = &OrderService{
			Repository: repository,
		}
	})
	return OrderServiceInstance
}

func (s *OrderService) Create(ctx *gin.Context, req *vo.OrderReq) (*model.Order, error) {
	instru, _ := repository.NewInstrumentRepository().SelectById(req.InstrumentID)
	// user, _ := ctx.Get("user")
	var user model.User
	db.GetDB().Where("id = ?", req.UserID).First(&user)
	orderModel := model.Order{
		InstrumentID:   req.InstrumentID,
		InstrumentName: instru.Name,
		Num:            req.Num,
		// UserID:         user.(model.User).ID,
		Status:        "待收货",
		InstrumentImg: instru.Img,
		UserID:        req.UserID,
		UserName:      user.Name,
		TransportWay:  req.TransportWay,
	}
	order, err := s.Repository.Create(&orderModel)
	if err != nil {
		// utils.LogrusObj.Error(err)
		return nil, err
	}
	return order, nil
}

func (s *OrderService) Retrieve(ctx *gin.Context, id int) (*model.Order, error) {
	order, err := s.Repository.SelectById(id)
	if err != nil {
		// utils.LogrusObj.Error(err)
		return nil, err
	}
	return order, nil
}

func (s *OrderService) Update(ctx *gin.Context, item *model.Order, requestItem *vo.OrderReq) (*model.Order, error) {
	order, err := s.Repository.Update(*item, *requestItem)
	if err != nil {
		// utils.LogrusObj.Error(err)
		return nil, err
	}
	return order, nil
}

func (s *OrderService) Delete(ctx *gin.Context, id int) error {
	err := s.Repository.DeleteById(id)
	if err != nil {
		// utils.LogrusObj.Error(err)
		return err
	}
	return nil
}

// OrderList 列举分类
func (s *OrderService) OrderList(ctx *gin.Context, req *vo.ListOrderReq) (resp *vo.DataListResp, err error) {
	orders, err := s.Repository.OrderList(req)
	if err != nil {
		// utils.LogrusObj.Error(err)
		return
	}
	Resp := make([]*vo.ListOrderResp, 0)
	for _, v := range orders {
		Resp = append(Resp, &vo.ListOrderResp{
			OrderNo:        v.OrderNo.String(),
			InstrumentID:   v.InstrumentID,
			InstrumentImg:  v.InstrumentImg,
			Num:            v.Num,
			Status:         v.Status,
			TransportWay:   v.TransportWay,
			InstrumentName: v.InstrumentName,
			UserID:         v.UserID,
			UserName:       v.UserName,
			CreatedAt:      v.CreatedAt.Local().Format("2006-01-02 15:04:05"),
		})
	}

	resp = &vo.DataListResp{
		Item:  Resp,
		Total: int64(len(Resp)),
	}

	return
}

func (s *OrderService) SelectByUserID(ctx *gin.Context, req *vo.ListOrderReq, id uint) (resp *vo.DataListResp, err error) {
	orders, err := s.Repository.SelectByUserID(req, id)
	if err != nil {
		// utils.LogrusObj.Error(err)
		return
	}
	Resp := make([]*vo.ListOrderResp, 0)
	for _, v := range orders {
		Resp = append(Resp, &vo.ListOrderResp{
			OrderNo:        v.OrderNo.String(),
			InstrumentID:   v.InstrumentID,
			InstrumentImg:  v.InstrumentImg,
			Num:            v.Num,
			Status:         v.Status,
			TransportWay:   v.TransportWay,
			InstrumentName: v.InstrumentName,
			UserID:         v.UserID,
			UserName:       v.UserName,
			CreatedAt:      v.CreatedAt.Local().Format("2006-01-02 15:04:05"),
		})
	}

	resp = &vo.DataListResp{
		Item:  Resp,
		Total: int64(len(Resp)),
	}

	return
}
